<template>
  <div class="note-block" ref="noteBlocking">
    <div class="noteScrolling-block" ref="noteScrollingBlock" @mouseover="handStop" @mouseleave="handStart">
      {{ value }}
    </div>
  </div>

</template>

<script>
export default {
  name: "scroll-nav",
  props: {
    value: {
      type: String,
      default: ""
    }
  },
  mounted() {
    this.startScroll()
  },
  methods: {
    startScroll() {
      const that = this
      this.adWidth = this.$refs.noteBlocking.clientWidth
      this.con1 = this.$refs.noteScrollingBlock
      this.conWidth = this.$refs.noteScrollingBlock.clientWidth
      this.con1.style.right = `0px`
      this.count = 0
      document.addEventListener("visibilitychange", function () {
        if (document.hidden === true) {
          that.handStop()
        } else {
          that.handStart()
        }
      });
      this.scroll()
    },
    handStop() {
      this.scroll(true)
    },
    handStart() {
      this.scroll(false)
    },
    scroll(flag) {
      clearTimeout(this.timeOutObj)
      if (flag) {
        return
      }
      this.timeOutObj = setTimeout(() => {
        this.count += 15
        this.con1.style.right = `${this.count}px`
        if (this.adWidth + this.conWidth + 20 < this.count) {
          this.con1.style.opacity = 0
          this.count = 0
          this.con1.style.right = `-0px`
          setTimeout(() => {
            this.con1.style.opacity = 1
            clearTimeout(this.timeOutObj)
            this.scroll(flag)
          }, 2000)
        } else {
          this.scroll(flag)
        }
      }, 500)
    }
  }
}
</script>

<style lang="less" scoped>
.note-block {
  position: relative;
  left: 0;
  right: 0;
  overflow: hidden;
  cursor: pointer;
  width: 100%;
  height: 100%;
  font-size: 14px;
  color: #ffffff;
  letter-spacing: -0.35px;

  .noteScrolling-block {
    height: 30px;
    line-height: 30px;
    transition: right 1s linear;
    display: inline-block;
    position: relative;
    right: 0;
    white-space: nowrap;
    transform: translate(100%, 0);
    cursor: pointer;
  }
}
</style>